package com.party.controller.user.vote;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.*;
import com.party.service.excellentparty.ExcellentPartyService;
import com.party.service.excellentparty.VotePersonService;
import com.party.service.excellentpartybranch.ExcellentPartyBranchService;
import com.party.service.news.NewsService;
import com.party.service.vote.VoPtionService;
import com.party.service.vote.VoteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:27
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping(value = "/vote")
public class VoteController {

    @Resource
    ExcellentPartyService excellentPartyService;

    @Resource
    ExcellentPartyBranchService excellentPartyBranchService;

    @Resource
    VotePersonService votePersonService;

    @Resource
    VoteService voteService;

    @Resource
    VoPtionService voPtionService;

    private int pageSize = 18;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/vote/index");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req,HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String creattime = req.getParameter("creattime");
        Vote vote = new Vote();
        int pageIndex = 1;

        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        vote.setTitle(title);
        vote.setCreattime(creattime);

        Page p = this.voteService.getVoteList(vote, pageSize, pageIndex);
        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "content")
    public ModelAndView content(
            @RequestParam(value = "voteid")Integer voteid,
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        String type = request.getParameter("type");
        List<VoPtion> voPtionList=null;

        Integer uid=-1;
        if ((SysUser) session.getAttribute("sysUser") != null) {
            uid = ((SysUser) session.getAttribute("sysUser")).getUid();
        }
        VotePerson votePerson=new VotePerson();
        votePerson.setUid(uid);
        votePerson.setVoteid(voteid);
        boolean flag=false;
        VotePerson votePerson1=votePersonService.getVotePersonByUid(votePerson);
        if(votePerson1!=null){
            flag=true;
        }
        Vote vote=voteService.getVoteByPrimaryKey(voteid);

        modelAndView.addObject("flag",flag);
        modelAndView.addObject("vote",vote);
        voPtionList=voPtionService.getAllVoPtionByVoteId(voteid);
        modelAndView.addObject("list",voPtionList);
        modelAndView.setViewName("user/vote/content");

        return modelAndView;
    }

    /**
     * 保持用户已经选择的
     * @param req
     * @return
     */
    @RequestMapping(value = "/saveSelect")
    @ResponseBody
    public Map<String,Object> saveSelect(HttpSession session,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String voteid=req.getParameter("voteid");
        String[] values = req.getParameterValues("voteName");
        Integer uid=-1;
        if ((SysUser) session.getAttribute("sysUser") != null) {
            uid = ((SysUser) session.getAttribute("sysUser")).getUid();
        }
        VotePerson votePerson=new VotePerson();
        votePerson.setUid(uid);
        votePerson.setVoteid(Integer.parseInt(voteid));
        VotePerson ret=votePersonService.getVotePerson(votePerson);

        /**
         * 设置当前人已投票,并投票
         */
        if(ret!=null){
            String state=ret.getState();
            if(!"".equals(state)&&"0".equals(state)){
                voPtionService.updateVoPtionVotesByIds(values);
                ret.setState("1");
                ret.setTime(Tools.getNowDate());
                ret.setVotenum(values.length);
                votePersonService.updateVotePersonByPrimaryKey(ret);
            }
        }

        map.put("status",true);
        return map;
    }
}
